#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Created on 2022-04-06 14:11:06
@author: DengLibin 榆霖
@description: 前向最大匹配算法
'''
from turtle import st

# 词库
class WordLibrary:
    
    def __init__(self) -> None:
        self.word_set = set(['我们', '经常', '有', '意见','分歧'])

    # 是否有某个词
    def have_word(self, word) -> bool:
        return word in self.word_set

# 分词
def participle(text, max_len) -> list:
    """分词
    """
    word_lib = WordLibrary()
    l = []
    length = len(text)
    start = 0
    end = max_len
    if end > length:
        end = length
    while start < length:
        word = text[start : end]
        if word_lib.have_word(word):
            l.append(word)
            start = end
            end = start + max_len
            if end > length:
                end = length
        else:
            end -= 1
        if end < start:
            start += 1
            end = start + max_len
            if end > length:
                end = length
    return l

if __name__ == '__main__':
    text = "我们经常有意见和分歧"
    print(participle(text, 3))